<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>高阶函数 柯里化</title>
</head>
<body>

<div id="test"></div>

<script src="js/react.development.js" crossorigin></script>
<script src="js/react-dom.development.js" crossorigin></script>
<script src="js/babel.js"></script>
<script type="text/babel">
    class Login extends React.Component {
        // 初始化状态
        state = {
            username: '',
            password: ''
        }

        // 函数柯里化写法
        saveFormData = (dataName) => {
            console.log(dataName);
            return (event) => {
                this.setState({[dataName]: event.target.value});
            }
        }

        // 不用函数柯里化实现
        ordinary = (key, value) => {
            this.setState({[key]: value});
        }

        submitCall = (event) => {
            /*阻止表单提交*/
            event.preventDefault();
            console.log(this.state.username);
        }

        render() {
            return (
                <form action="https://www.baidu.com" onSubmit={this.submitCall}>
                    USERNAME: <input onChange={this.saveFormData('username')} type="text" name="username"/>
                    PASSWORD: <input onChange={(event) => {this.ordinary('password',event.target.value)}} type="password" name="password"/>
                    <button>Login</button>
                </form>
            );
        }
    }

    ReactDOM.render(<Login/>, document.getElementById('test'))
</script>
</body>
</html>